import { EBundleType, EGameType, EViewType } from "../../Script/Common/Enum";
import EventID from "../../Script/Common/EventID";
import { GlobalData } from "../../Script/Common/GlobalData";
import { TalkModule } from "../../Script/Common/TalkModule";
import { IGameData, IMessage, ITalkData, IViewData } from "../../Script/Interface/IMessage";
import { SceneGame } from "../../Script/SceneGame";
import AppService from "../../Script/Service/AppService";
import GameUtil from "../../Script/Util/GameUtil";
import StorageUtil from "../../Script/Util/StorageUtil";
import BaseLayer from "../Base/BaseLayer";
import AudioManager, { EMusicType } from "../Util/AudioManager";

const { ccclass, property } = cc._decorator;

const VIEW_DATA: IViewData = {
    name: 'LayerGame',
    type: EViewType.Layer,
    bundle: EBundleType.Game,
    zIndex: 100,
};

@ccclass
export class LayerGame extends BaseLayer implements IMessage {
    public static instance: LayerGame = null;

    @property({ type: [cc.Node], tooltip: "故事前情/家" })
    public views: cc.Node[] = [];

    private gameData: IGameData = null;
    private talkData: ITalkData = null;

    recvMsg(cmd: number, data: any) {
        switch (cmd) {
            case EventID.TEST_CMD:
                {
                    
                }
                break;
        }
    }

    protected onLoad() {
        super.onLoad();
        this.addMessage(EventID.TEST_CMD, this);

        LayerGame.instance = this;

        this.gameData = StorageUtil.instance.getGameData();
        this.talkData = StorageUtil.instance.getTalkData();
        
        this.initLayer();
    }

    public async initLayer() {
        await GameUtil.sleep(1000 / 60);
        this.views.forEach((o, i) => {
            o.active = i == GlobalData.gameView;
            switch (GlobalData.gameView) {
                case EGameType.Home:
                    AudioManager.instance.playMusic(EMusicType.HomeBgm, 0.4);
                    break;
                case EGameType.Town:
                    AudioManager.instance.playMusic(EMusicType.TownBgm, 0.8);
                    break;
            }
        });
        // 更新
        TalkModule.updateCheck();
        // 展示游戏UI层
        SceneGame.instance.showTopUI();
    }

    public checkBagHad(datas: { id: number, num: number }[]) {
        for (let i = 0; i < datas.length; i++) {
            const data = datas[i];
            const find = GlobalData.getBagDataById(data.id);
            if (!find || find?.num < data.num) {
                return false;
            }
        }
        return true;
    }

    private onBtnsClicked(event: cc.Event.EventTouch, eventData: string) {
        const name = event.target.name;
        switch (name) {
            case 'btnTip':
                {
                    // this.tipView.active = true;
                    // const data = GlobalData.getGameTips();
                    // const lab = cc.find("root/labDesc", this.tipView).getComponent(cc.Label);
                    // lab.string = `${data.tips}`;
                }
                break;
            case 'btnCloseTip':
                {
                    // this.tipView.active = false;
                }
                break;
        }
    }

    public getViewData() {
        return VIEW_DATA;
    }
}
AppService.instance.registViewData(VIEW_DATA);